const express = require("express");
const router = express.Router();
const {server} = require("../../controllers");
const { verifyToken } = require("../verifyToken");

// 参数验证中间件
const validateArticleId = (req, res, next) => {
  if (!req.body.id) {
    return res.status(400).json({error: '文章ID必填'});
  }
  next();
};

// 获取文章
router.post('/getArticle', (req, res) => server.getArticle(req, res));
// 更新文章状态
router.post('/updateArticleState', verifyToken, (req, res) => server.updateArticleState(req, res));
// 删除文章
router.post('/deleteArticle', verifyToken, (req, res) => server.deleteArticle(req, res));
// 获取文章不同状态下的条数
router.post('/getArticleStateCount', verifyToken, (req, res) => server.getArticleStateCount(req, res));
// 新建文章、图库
router.post('/addArticle', verifyToken, (req, res) => server.addArticle(req, res));
// 通过id获取文章详情
router.post('/getArticleById', validateArticleId, (req, res) => server.getArticleById(req, res));
// 修改文章
router.post('/updateArticle', verifyToken, (req, res) => server.updateArticle(req, res));
//修改文章时获取文章内容
router.post('/getArticleById', verifyToken, validateArticleId, (req, res) => server.getArticleById(req, res));

module.exports = router;